#coding=utf-8
import urllib,time,decimal
from BeautifulSoup import BeautifulSoup

def get_soup(url, coding='gb18030'):
    import socket
    socket.setdefaulttimeout(10)
    while True:
        try:
            f = urllib.urlopen(url)
            htmltext = f.read().decode(coding, 'ignore')
            s = BeautifulSoup(htmltext) 
            return s
        except (socket.timeout,IOError), e:
            print url, e
            time.sleep(0.1)

def data2csv(data, head, fn):
    f = open(fn, 'w')
    
    h = ','.join(head)
    h = h + '\n'
    f.write(h)
    
    i = 1
    for d in data:
        s = ','.join(d)
        s = s + '\n'
        f.write(s)
        
        if i%10 == 0:f.flush()
        i = i + 1
    
    f.close()

def csv2data(fn):
    data = []
    
    f = open(fn)
    
    lines = f.readlines()
    lines = lines[1:]
    
    for line in lines:
        line = line[:-1]
        d = line.split(',')
        data.append(d)
    
    return data   

def stcode_from_csv(fn):
    data = csv2data(fn)
    return [d[0] for d in data]
    
def code_name_dict(fn):
    data = csv2data(fn)
    return dict(data)
    
def float2dec(f, place):

    q = decimal.Decimal( (0, (1, ), -place) )
    r = decimal.Decimal(str(f)).quantize(q)
    return r
     
        
